package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Oilspec;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.OilspecService;

@Controller
@RequestMapping("/")
public class OilspecController {
	
	//自动注入
	@Autowired
	private OilspecService oilspecService;
	
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	/**
	 * 根据公司id查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryOilsperAll/look.action")
    public String queryOilsperAll(HttpServletRequest request, String flag) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//根据公司id查询
		List<Oilspec> oilspecs = oilspecService.queryOilsperByFcontrolunitid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("oilspecs", oilspecs);
        request.setAttribute("flag", flag);
        return "/backHome/basicData/oilspec/oilspecList";
	}
	
	/**
	 * 根据规格和公司id查询
	 * @param request
	 * @param fnameL2
	 * @return
	 */
	@RequestMapping("/queryOilspetByFnameL2")
	public String queryOilspetByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		if("".equals(fnameL2)){
            queryOilsperAll(request, null);
        }else{
            fnameL2 = fnameL2.trim();//去空格
            //查询到的数据
			List<Oilspec> oilspecs = oilspecService.queryOilsperByFcontrolunitidAndFnameL2(user.getFcompanyid(), fnameL2);
			//把数据传到页面
			request.setAttribute("oilspecs", oilspecs);
			request.setAttribute("fnameL2", fnameL2);
		}
		return "/backHome/basicData/oilspec/oilspecList";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertOilspet")
	public String insertOilspet(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取上一个编码
        /*String fnumber = oilspecService.selectFnumber(user.getFcompanyid());*/
		//获取编码
        String number = GetFnumber.fnumbers("QYGG");
		//把数据传到页面
		request.setAttribute("fnumber", number);
		request.setAttribute("type", type);
		return "/backHome/basicData/oilspec/addOilspec";
	}

	/**
	 * 实现添加汽油规格
	 * @param request
	 * @param oilspec 属性自动封装
	 * @return
	 */
	@RequestMapping("/addOilspec")
	public String addOilspec(HttpServletRequest request,Oilspec oilspec){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//利用UUID生成fid
		oilspec.setFid(GetFid.UUIDIntoFid());
		//控制单元id
		oilspec.setFcontrolunitid(user.getFcompanyid());
		//创建人id
		oilspec.setFcreatorid(user.getFid());
	    //获取当前时间
	  	String createTime = TimeFormat.timeFormat(new Date());
	  	//当前创建时间
	  	oilspec.setFcreatetime(createTime);
	  	//添加
	  	oilspecService.addOilspec(oilspec);
	    //添加日志
	    customLogService.addLog("车辆基础资料", createTime+"对汽油规格进行添加操作", null, user.getFid());
	    //重定向到添加方法
	    return "redirect:/DrivingSys/insertOilspet?type=3";
	}
	
	/**
	 * 实现删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteOilspecByFid")
	public String deleteOilspecByFid(HttpServletRequest request,String deleteId[],String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//删除
		if(fid == null || "".equals(fid)){
			for(int i = 0;i<deleteId.length;i++){
				oilspecService.deleteOilspecByFid(deleteId[i]);
			}
		}else{
			oilspecService.deleteOilspecByFid(fid);
		}
		
		 //添加日志
	    customLogService.addLog("车辆基础资料", TimeFormat.timeFormat(new Date())+"对汽油规格进行删除操作", null, user.getFid());
	    //调用查询页面
        queryOilsperAll(request, null);
        return "/backHome/basicData/oilspec/oilspecList";
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid 要查询的fid
	 * @return
	 */
	@RequestMapping("/selectOilspecByFid")
	public String selectOilspecByFid(HttpServletRequest request,String fid){
		//查询到的数据
		Oilspec oilspec = oilspecService.queryByPrimaryKey(fid);
		//把数据传到页面
		request.setAttribute("oilspec", oilspec);
		return "/backHome/basicData/oilspec/updateOilspec";
	}
	
	/**
	 * 修改
	 * @param request
	 * @param oilspec 属性自动封装
	 * @return
	 */
	@RequestMapping("/modifyOilspec")
	public String modifyOilspec(HttpServletRequest request,Oilspec oilspec){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//控制单元id
		oilspec.setFcontrolunitid(user.getFcompanyid());
		//创建人id
		oilspec.setFlastupdateuserid(user.getFid());
	    //获取当前时间
	    String updateTime = TimeFormat.timeFormat(new Date());
	    //当前创建时间
	    oilspec.setFlastupdatetime(updateTime);
	    //修改
	    oilspecService.modifyOilspec(oilspec);
	    //添加日志
	    customLogService.addLog("车辆基础资料", updateTime+"对汽油规格进行修改操作", null, user.getFid());
	    //调用查询页面
        return commonController.skipUrl("/queryOilsperAll/look.action");
    }


	/**
	 * 导出Execl
	 *
	 * @param response
	 */
	@RequestMapping("/importOilSpec")
	public void importTrainType(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		List<Object> data = new ArrayList<>();
		//查询数据
		List<Oilspec> oilspecs = null;
		if("".equals(fnameL2)){
			oilspecs = oilspecService.queryOilsperByFcontrolunitid(user.getFcompanyid());
		}else {
            oilspecs = oilspecService.queryOilsperByFcontrolunitidAndFnameL2(user.getFcompanyid(), fnameL2.trim());
        }
		for (Oilspec oilspec : oilspecs) {
			data.add(oilspec.getFnumber());//编号
			data.add(oilspec.getFnameL2());//名称
			data.add(oilspec.getFsimplename());//班级简称
			data.add(oilspec.getFdescriptionL2());//描述
			data.add(oilspec.getFcreatetime());//创建时间
			data.add(oilspec.getFlastupdatetime());//最后修改时间
			data.add(oilspec.getCreateName());//创建人
			data.add(oilspec.getUpdateName());//修改人
		}

		//头部数据
		String head[] = {"编码", "规格", "简称", "描述", "创建时间", "最后修改时间","创建人", "修改人"};
		//长度
		Integer length[] = {5000, 7000, 5000, 5000, 6200, 6200, 3000, 3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "汽油规格" + TimeFormat.getNowTime2() + ".xls", "汽油规格", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
